//
//
//  BigTyre
//
//  Created by 张钧 on 2017/3/23.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "MySigninViewController.h"
#import "MyCommon.h"
#import "MysetViewController.h"
#import <ShareSDK/ShareSDK.h>
@interface MySigninViewController ()
{
    MyZcVIew * zcView;  //分段控制器的注册界面
    MySigninView * signView;//分段控制器的登陆界面
}
@end

@implementation MySigninViewController
-(void)viewWillAppear:(BOOL)animated
{
    //隐藏导航栏
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏导航栏
    self.view.backgroundColor=[UIColor redColor];
    
    //背景图片
    UIImageView * bgimage=[[UIImageView alloc]initWithFrame:self.view.frame];
    bgimage.image=[UIImage imageNamed:@"IMG_0394"];
    bgimage.userInteractionEnabled = YES;
    [self.view addSubview:bgimage];
    
    //左上角返回按钮
    UIButton * backbt=[[UIButton alloc]initWithFrame:CGRectMake(20, 40, 30, 30)];
    [backbt addTarget:self action:@selector(backMyView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backbt];
    
    //分段控制器
    NSArray * sgarr=@[@"登陆",@"注册"];
    UISegmentedControl * segment=[[UISegmentedControl alloc]initWithItems:sgarr];
    segment.frame=CGRectMake((self.view.frame.size.width-150)/2, (self.view.frame.size.height-150)/2, 150, 30);
    segment.tintColor=[UIColor blackColor];
    segment.selectedSegmentIndex=0;     /*分段控制器默认选中*/
    [segment addTarget:self action:@selector(jump:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segment];
    
    //分段控制器的登陆界面
    signView=[[MySigninView alloc]initWithFrame:CGRectMake(50, (self.view.frame.size.height-75)/2, self.view.frame.size.width-100, 120)];
//    signView.backgroundColor=[UIColor cyanColor];
    [signView.hq addTarget:self action:@selector(clickPush) forControlEvents:UIControlEventTouchUpInside];
    [signView.forget addTarget:self action:@selector(ts) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:signView];
    
   //分段控制器的注册界面
    zcView=[[MyZcVIew alloc]initWithFrame:CGRectMake(50, (self.view.frame.size.height-75)/2, self.view.frame.size.width-100, 120)];
    [zcView.hq addTarget:self action:@selector(clickPush) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zcView];
    
    //隐藏注册界面
    zcView.hidden=YES;
    
    //登陆按钮
    UIButton * button=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-30)/2,( self.view.frame.size.height)/2+100, 40, 40)];
    [button setBackgroundImage:[UIImage imageNamed:@"右箭头"] forState:0];
    [button addTarget:self action:@selector(signin) forControlEvents:UIControlEventTouchUpInside];
    button.layer.masksToBounds=YES;
    button.layer.cornerRadius=20;
    [self.view addSubview:button];
    
 
//#pragma mark-第三方登录
    //微信
    UIButton * weixin=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/5-15, self.view.frame.size.height-150, 30, 30)]; /*x轴位置=(本类宽度 / 5 - 按钮宽度 /2)  */
    [weixin setBackgroundImage:[UIImage imageNamed:@"微信"] forState:0];
    //    [weixin setBackgroundColor:[UIColor blackColor]];
    [weixin addTarget:self action:@selector(clickwx) forControlEvents:UIControlEventTouchUpInside];
    weixin.layer.cornerRadius=15;
    weixin.layer.masksToBounds=YES;
    
    [bgimage addSubview:weixin];
    //微博
    UIButton * weibo=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width/5+self.view.frame.size.width/5)-15, self.view.frame.size.height-150, 30, 30)];
    [weibo setBackgroundImage:[UIImage imageNamed:@"新浪"] forState:0];
    [weibo addTarget:self action:@selector(clickwb) forControlEvents:UIControlEventTouchUpInside];
    //    [weibo setBackgroundColor:[UIColor blackColor]];
    weibo.layer.cornerRadius=15;
    weibo.layer.masksToBounds=YES;
    [bgimage addSubview:weibo];
    //腾讯
    UIButton * qq=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width/5*3)-15, self.view.frame.size.height-150, 30, 30)];
    [qq setBackgroundImage:[UIImage imageNamed:@"QQ"] forState:0];
    //    [qq setBackgroundColor:[UIColor blackColor]];
    [qq addTarget:self action:@selector(clickqq) forControlEvents:UIControlEventTouchUpInside];
    qq.layer.cornerRadius=15;
    qq.layer.masksToBounds=YES;
    [bgimage addSubview:qq];
    //连输
    UIButton * facebook=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width/5*4)-15, self.view.frame.size.height-150, 30, 30)];
    [facebook setBackgroundImage:[UIImage imageNamed:@"facebook"] forState:0];
    //    [facebook setBackgroundColor:[UIColor blackColor]];
    [facebook addTarget:self action:@selector(clickfb) forControlEvents:UIControlEventTouchUpInside];
    facebook.layer.cornerRadius=15;
    facebook.layer.masksToBounds=YES;
    [self.view addSubview:facebook];
    
}
-(void)clickfb
{
  
    [ShareSDK getUserInfo:SSDKPlatformTypeFacebook
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {  UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
             [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                 MyViewController * view=[[MyViewController alloc]init];
                 [self.navigationController popViewControllerAnimated:view];
                 
             }]];
             [self presentViewController:alert animated:NO completion:^{
                 
             }];
         }
         else
         {
             NSLog(@"%@",error);
         }
     }];
}
-(void)clickwx
{
    
    NSLog(@"aaaa");
    [ShareSDK getUserInfo:SSDKPlatformTypeWechat
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {  UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
             [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                 [self.navigationController popViewControllerAnimated:YES];
             }]];
             [self presentViewController:alert animated:NO completion:^{
              
             }];
         }
         else
         {
             NSLog(@"%@",error);
         }
     }];
}
-(void)clickwb
{
    //例如微博的登录
    [ShareSDK getUserInfo:SSDKPlatformTypeSinaWeibo
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             
             UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
             [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                [self.navigationController popViewControllerAnimated:YES];

                 
                 
             }]];
             [self presentViewController:alert animated:NO completion:^{
             }];
//             NSLog(@"uid=%@",user.uid);
//             NSLog(@"%@",user.credential);
//             NSLog(@"token=%@",user.credential.token);
//             NSLog(@"nickname=%@",user.nickname);
          
        
         }
         
         else
         {
             NSLog(@"%@",error);
         }
         
     }];
}
-(void)clickqq
{
    
    [ShareSDK getUserInfo:SSDKPlatformTypeQQ
           onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error)
     {
         if (state == SSDKResponseStateSuccess)
         {
             
             NSLog(@"!@#$%^&*()_");
             UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
             [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                  [self.navigationController popViewControllerAnimated:YES];
             }]];
             [self presentViewController:alert animated:NO completion:^{
     
                 
             }];

         }
         
         else
         {
             NSLog(@"++++++%@",error);
         }
         
     }];
}
//分段控制器跳转
-(void)jump:(UISegmentedControl *)ment
{
    
    switch (ment.selectedSegmentIndex) {
        case 0:
            signView.hidden=NO;
            zcView.hidden=YES;
            break;
        case 1:
            signView.hidden=YES;
            zcView.hidden=NO;
            break;
        default:
            break;
    }
}
//获取验证码的跳转
-(void)clickPush
{
    
    MySelectedViewController * selected=[[MySelectedViewController alloc]init];
    [self.navigationController pushViewController:selected animated:NO];
}
//键盘事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //点击页面任意部分取消显示键盘
    [self.view endEditing:YES];
}
//登陆响应
-(void)signin
{
    /* 如果手机号输入框长度为0，提示错误！如果不为0   →*/
    if (signView.zhTF.text.length==0) {
        UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"请输入手机号" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [self presentViewController:alert animated:YES completion:^{
        }];
    }
    /*如果手机号输入框长度＜11或＞11，提示请输入完整！*/
    else if (signView.zhTF.text.length<11||signView.zhTF.text.length>11) {
        UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"请将手机号输入完整" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }
    /*如果手机号输入长度等于11 就检测密码长度是否正确，正确就跳转，错误就提示输入密码*/
    else if(signView.zhTF.text.length==11)
    {
        if (signView.mmTF.text.length==0) {
            UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"请重新输入密码" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }]];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
        else {
            HomeViewController * home=[[HomeViewController alloc]init];
            [self presentViewController:home animated:NO completion:^{
            
        }];
        }
    }

}
-(void)backMyView
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)ts
{
    /*底部弹出警告*/
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"找回密码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        Myforget * forget=[Myforget new];
        [self.navigationController pushViewController:forget animated:YES];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"验证手机号码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        Myforget * forget=[Myforget new];
        [self.navigationController pushViewController:forget animated:YES];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alert animated:YES completion:^{}];
}


@end
